import { Effect, ImmerReducer, Reducer, Subscription,history } from 'umi';
import {login} from '@/api/login'
import { message } from "antd";
export interface IndexModelState {
  name: string;
}

export interface IndexModelType {
  namespace: 'index';
  state: IndexModelState;
  effects: {
    query: Effect;
  };
  reducers: {
    save: Reducer<IndexModelState>;
  };
  subscriptions: { setup: Subscription };
}

const IndexModel: any = {
  namespace: 'getlogin',
  state: {
    name: '',
  },

  effects: {
    *getLogin({ payload }:any, { call, put }:any):any {

     const {name,password}=payload
     const result:any=yield call(login,name,password)
     console.log(result);
     yield put({
      type:"setLogin",
      payload:result.data.data
     })
     if(result.status===200){
    localStorage.setItem('token', result.data.data.token)
    localStorage.setItem('user', JSON.stringify(result.data.data))
    history.push('/pages/Workbench')
    message.success('登录成功')
     }else if(result.status===400){
      message.error('账号或密码输入错误')
     }
    },

  },
  reducers: {
    setLogin(state:any, action:any) {

      return {
        ...state,
        ...action.payload,
      };
    },
  }

};

export default IndexModel;
